package edu.ccut.saturn.workflow.query;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;

public class GetDeptIdsByLoginUserId extends DefaultComponent {

	@SuppressWarnings("unchecked")
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		String userSerialno = (String) parameterContext.getParameterValue(0);
		Connection conn = parameterContext.getConnection();
		List<String> deptIds = getUsersByActivityInsIds(conn, userSerialno);
		parameterContext.setParameter(1, deptIds);
	}

	/**
	 * 根据用户id集合得到部门code集合
	 * 
	 * @param conn
	 * @param userIds
	 * @return
	 */
	private List<String> getUsersByActivityInsIds(Connection conn,
			String userSerialno) {
		List<String> deptIds = new ArrayList<String>();
		Statement stm = null;
		ResultSet rs = null;
		try {
			stm = conn.createStatement();
			String sql = "select code from saturn_authority_department where id =(select dept_id from saturn_authority_user where id ='"
					+ userSerialno + "')";
			rs = stm.executeQuery(sql);
			while (rs.next()) {
				deptIds.add(rs.getString(1));
			}
		} catch (Exception e) {

		} finally {
			try {
				rs.close();
				stm.close();
			} catch (Exception e) {

			}
		}
		return deptIds;
	}
}
